/**
 * 
 */
package com.qolome.service.impl;

import static com.qolome.dao.mapper.PasswdBookDynamicSqlSupport.accountId;
import static com.qolome.dao.mapper.PasswdBookDynamicSqlSupport.accountNumber;
import static com.qolome.dao.mapper.PasswdBookDynamicSqlSupport.ownId;
import static com.qolome.dao.mapper.PasswdBookDynamicSqlSupport.passwdBook;
import static com.qolome.dao.mapper.PasswdBookDynamicSqlSupport.remark;
import static com.qolome.dao.mapper.PasswdBookDynamicSqlSupport.serverName;
import static com.qolome.dao.mapper.PasswdBookDynamicSqlSupport.type;
import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo;
import static org.mybatis.dynamic.sql.SqlBuilder.select;

import java.util.List;
import java.util.stream.Collectors;

import org.mybatis.dynamic.sql.render.RenderingStrategies;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.qolome.dao.mapper.PasswdBookMapper;
import com.qolome.dao.model.PasswdBook;
import com.qolome.dao.model.User;
import com.qolome.dto.request.AccountDtoCreateRequest;
import com.qolome.dto.request.AccountDtoQueryRequest;
import com.qolome.dto.request.AccountGetPassword;
import com.qolome.enums.ResultEnum;
import com.qolome.exception.QolomeException;
import com.qolome.service.AccountService;
import com.qolome.service.UserService;
import com.qolome.utils.HashUtil;
import com.qolome.utils.IdUtil;
import com.qolome.utils.security.AESUtils;
import com.qolome.utils.web.HttpResult;

import lombok.extern.slf4j.Slf4j;

/**
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author 姜锋
 * @date 2020年1月4日 下午1:52:02 
 * @version V1.0   
 *
 */
@Service
@Slf4j
public class AccountServiceImpl implements AccountService{

	@Autowired
	PasswdBookMapper passwdBookMapper;
	
	@Autowired
	UserService userService;
	
	
	@Override
	@Transactional
	public void create(AccountDtoCreateRequest request) {
		
		User user = userService.selectByPrimaryKey(request.getUserId());
		passwdBookMapper.insertMultiple(request.getList().stream().map(passwdBook->{
			Long uid = IdUtil.nextId();
			passwdBook.setAccountId(uid);
			passwdBook.setOwnId(request.getUserId());
			passwdBook.setAccountPassword(
					AESUtils.aesEncryptStr(
							passwdBook.getAccountPassword(),
							user.getUserKey(),
							uid.toString().getBytes()));
			return passwdBook;
		}).collect(Collectors.toList()));
		
	}
	
	@Override
	public List<PasswdBook> list(AccountDtoQueryRequest accountDtoQueryRequest) {
		SelectStatementProvider selectStatement  = 
				select(accountId,serverName,accountNumber,type,remark)
				.from(passwdBook)
				.where(ownId,isEqualTo(accountDtoQueryRequest.getUserId()))
				.build()
				.render(RenderingStrategies.MYBATIS3);
		return passwdBookMapper.selectMany(selectStatement);
	}

	/* （非 Javadoc）
	 * @see com.qolome.service.AccountService#getPassword(com.qolome.dto.request.AccountGetPassword)
	 */
	@Override
	public HttpResult<PasswdBook> getPassword(AccountGetPassword request) {
		User user = userService.selectByPrimaryKey(request.getUserId());
		
		PasswdBook passwdBook = passwdBookMapper.selectOne(
				completer->completer
				.where(accountId, isEqualTo(request.getAccountId()))
				.and(ownId, isEqualTo(request.getUserId()))
				).orElseThrow(()->new QolomeException(ResultEnum.ACCOUNT_NOT_FUND));
		
		passwdBook.setAccountPassword(
				AESUtils.aesDecodeStr(
						passwdBook.getAccountPassword(),
						user.getUserKey(),
						passwdBook.getAccountId().toString().getBytes())
				);
		return HttpResult.success(passwdBook, null);
	}

	
	@Override
	public void updatePassword() {
		
		List<PasswdBook> passwdBookList = passwdBookMapper.select(c->c);
		passwdBookList.forEach(passwdBook->{
			String password = AESUtils.aesDecodeStr(
					passwdBook.getAccountPassword(),
					HashUtil.getMd5("JF@Qolome5495").substring(16),
					passwdBook.getAccountId().toString().getBytes());
					log.info("密码:{}",password);
					});
		
		
	}
}
